package scantask.containers;

import gov.nasa.worldwind.layers.AirspaceLayer;

import java.util.Map;

import scantask.client.threeDObjects.LeafPolygone;
import scantask.client.threeDObjects.PolygonePool;
import scantask.client.threeDObjects.exceptions.PolygoneCreationException;
import scantask.containers.exceptions.IllegalLocationNameExeption;
import scantask.containers.exceptions.MissingLayerException;
import scantask.containers.exceptions.UnInitializeObjectException;

public abstract class Measurement extends InformationObject{
	
	protected double _value;
	protected WeekDate _date;
	protected String _locationName;
	

	public Measurement(Coordinate coordinate, String locationName, ZoomLevel viewInterval, double value, WeekDate date) throws UnInitializeObjectException, IllegalLocationNameExeption {
		super(coordinate, viewInterval);
		
		if ((locationName == null) || (locationName.compareTo("") == 0)) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed create mesurement illegal location name");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed create mesurement illegal location name");
			throw new IllegalLocationNameExeption();
		}
		this._locationName = locationName;
		/** TODO value can be less then 0 ??? like temperature **/
		this._value = value;
		this._date = date;
	}
	
	public Measurement(Coordinate coordinate, String locationName, ZoomLevel viewInterval, PolygonePool pool, double value, WeekDate date) throws UnInitializeObjectException {
		super(coordinate, viewInterval, pool);
		
		this._locationName = locationName;
		/** TODO value can be less then 0 ??? like temperature **/
		this._value = value;
		this._date = date;
	}
	
	public double getValue() {
		return this._value;
	}
	
	public WeekDate getDate() throws UnInitializeObjectException {
		if (this._date == null) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed WeekDate object is null");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed WeekDate object is null");
			throw new UnInitializeObjectException();
		}
		
		return this._date;
	}
	
	public String getLocationName() {
		return this._locationName;
	}
	
	public void setValue(double value) {
		this._value = value;
	}
	
	public void setDate(WeekDate date) {
		this._date = date;
	}
	
	public void setLocationName(String locationName) {
		this._locationName = locationName;
	}

	@Override
	public void buildDisplayedObject(PolygonePool polygonePool) throws PolygoneCreationException {
		this._polygone = polygonePool.getPolygone(this.getType());
		this.setDefaultPolygoneAccordingly();
	}
	
	private void setDefaultPolygoneAccordingly() {
		this._polygone.setLocation(this.getCoordinate());
		/** Measurement object MUST be LeafPolygone **/
		((LeafPolygone)this._polygone).addValueToUp((int)this.getValue());
		((LeafPolygone)this._polygone).setValue(this.getValue());
	}
	
	@Override
	public void init3DObject(Map<String, AirspaceLayer> layers) throws MissingLayerException {
		AirspaceLayer layer = layers.get(this.getType());
		if (layer == null) {
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Missing layer " + this.getType() + " exception");
			throw new MissingLayerException();
		}
		this._polygone.init3DObject(layer);
		this._polygone.setDescription(this.getToolTipDescription());
	}

}
